package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.InviteRecord;
import com.ruoyi.system.mapper.InviteRecordMapper;
import com.ruoyi.system.mapper.InviteeRecordMapper;
import com.ruoyi.system.service.InviteRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.UUID;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

@Service
public class InviteRecordServicelmpl implements InviteRecordService {

    @Autowired
    private InviteRecordMapper inviteRecordMapper;

    @Override
    public InviteRecord selectByInviteCode(String inviteCode) {
        return inviteRecordMapper.selectByInviteCode(inviteCode);
    }

    @Override
    public String insertInviteRecord(InviteRecord record) {
        String code = "CODERNIGHT" + UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 8).toUpperCase();
        record.setInviteCode(code);
        record.setCreateTime(DateUtils.getNowDate());
        record.setExpireTime(DateUtils.getDateAfterDays(3));
        record.setInviterId(getUserId());
        record.setStatus(0);
        inviteRecordMapper.insertInviteRecord(record);
        return code;
    }

    @Override
    public int updateInviteRecord(InviteRecord record) {
        record.setStatus(3);
        return inviteRecordMapper.updateInviteRecord(record);
    }
}
